package com.thinkgem.jeesite.modules.fantasy.service.impl;

import java.util.List;

import com.thinkgem.jeesite.modules.fantasy.common.EasyUIPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.fantasy.dao.CityMapDao;
import com.thinkgem.jeesite.modules.fantasy.entity.CityMap;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Service
public class CityMapService extends CrudService<CityMapDao, CityMap> {
	@Autowired
	public CityMapDao cityMapDao;
	
	/**
	 * 获取城市地点列表
	 * @param easyUIPage
	 * @return
	 */
	public List<CityMap> queryCityMaplistPage(EasyUIPage easyUIPage){
		List<CityMap> list = cityMapDao.queryCityMaplistPage(easyUIPage);
		for (CityMap cityMap : list) {
			if (cityMap.getUpdateBy() != null) {
				User user = UserUtils.get(cityMap.getUpdateBy().getId());
				cityMap.setUpdateBy(user);
			}
		}
		return list;
	}
	/**
	 * 根据地点id查询地点信息
	 * @param mapId
	 * @return
	 */
	public CityMap getCityMapById(String mapId) {
		CityMap cityMap = cityMapDao.getCityMapById(mapId);
		return cityMap;
	}
	/**
	 * 根据城市id获取相关所有地点信息
	 * @param cityId
	 * @return
	 */
	public List<CityMap> getCityMapsByCityId(String cityId) {
		return cityMapDao.getCityMapsByCityId(cityId);
	}

	@Transactional(readOnly = false)
	public Integer saveCityMap(CityMap cityMap) {
		Integer result = -1;
		// TODO Auto-generated method stub
		if (null == cityMap) {
			logger.debug("待保存的cityMap不能为空！");
			return result;
		} else {
			//西北
			if (cityMap.getNorthwest() != null && cityMap.getNorthwest().equals("-1")) {
				if (cityMap.getNorthwestInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getNorthwestInput());
					newNode.setPlacedesc("");
					newNode.setSoutheast(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("西北方向新地点id = " + newNode.getId());
						cityMap.setNorthwest(newNode.getId());
					} else {
						logger.debug("西北方向新地点创建失败！");
					}
				} else {
					logger.debug("西北方向新地点不能为空！");
				}
			}
			//正北
			if (cityMap.getNorth() != null && cityMap.getNorth().equals("-1")) {
				if (cityMap.getNorthInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getNorthInput());
					newNode.setPlacedesc("");
					newNode.setSouth(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("正北方向新地点id = " + newNode.getId());
						cityMap.setNorth(newNode.getId());
					} else {
						logger.debug("正北方向新地点创建失败！");
					}
				} else {
					logger.debug("正北方向新地点不能为空！");
				}
			}
			//东北
			if (cityMap.getNortheast() != null && cityMap.getNortheast().equals("-1")) {
				if (cityMap.getNortheastInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getNortheastInput());
					newNode.setPlacedesc("");
					newNode.setSouthwest(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("东北方向新地点id = " + newNode.getId());
						cityMap.setNortheast(newNode.getId());
					} else {
						logger.debug("东北方向新地点创建失败！");
					}
				} else {
					logger.debug("东北方向新地点不能为空！");
				}
			}
			//正西
			if (cityMap.getWest() != null && cityMap.getWest().equals("-1")) {
				if (cityMap.getWestInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getWestInput());
					newNode.setPlacedesc("");
					newNode.setEast(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("正西方向新地点id = " + newNode.getId());
						cityMap.setWest(newNode.getId());
					} else {
						logger.debug("正西方向新地点创建失败！");
					}
				} else {
					logger.debug("正西方向新地点不能为空！");
				}
			}
			//正东
			if (cityMap.getEast() != null && cityMap.getEast().equals("-1")) {
				if (cityMap.getEastInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getEastInput());
					newNode.setPlacedesc("");
					newNode.setWest(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("正东方向新地点id = " + newNode.getId());
						cityMap.setEast(newNode.getId());
					} else {
						logger.debug("正东方向新地点创建失败！");
					}
				} else {
					logger.debug("正东方向新地点不能为空！");
				}
			}
			//西南
			if (cityMap.getSouthwest() != null && cityMap.getSouthwest().equals("-1")) {
				if (cityMap.getSouthwestInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getSouthwestInput());
					newNode.setPlacedesc("");
					newNode.setNortheast(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("西南方向新地点id = " + newNode.getId());
						cityMap.setSouthwest(newNode.getId());
					} else {
						logger.debug("西南方向新地点创建失败！");
					}
				} else {
					logger.debug("西南方向新地点不能为空！");
				}
			}
			//正北
			if (cityMap.getSouth() != null && cityMap.getSouth().equals("-1")) {
				if (cityMap.getSouthInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getSouthInput());
					newNode.setPlacedesc("");
					newNode.setNorth(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("正北方向新地点id = " + newNode.getId());
						cityMap.setSouth(newNode.getId());
					} else {
						logger.debug("正北方向新地点创建失败！");
					}
				} else {
					logger.debug("正北方向新地点不能为空！");
				}
			}
			//东北
			if (cityMap.getSoutheast() != null && cityMap.getSoutheast().equals("-1")) {
				if (cityMap.getSoutheastInput() != null) {
					CityMap newNode = new CityMap();
					newNode.setCityid(cityMap.getCityid());
					//newNode.setCity(cityMap.getCity());
					newNode.setPlacename(cityMap.getSoutheastInput());
					newNode.setPlacedesc("");
					newNode.setNorthwest(cityMap.getId());
					//创建新节点标志
					int flag = -1;
					flag = this.save(newNode);
					if (flag == 1) {
						logger.debug("东北方向新地点id = " + newNode.getId());
						cityMap.setSoutheast(newNode.getId());
					} else {
						logger.debug("东北方向新地点创建失败！");
					}
				} else {
					logger.debug("东北方向新地点不能为空！");
				}
			}
			result = this.save(cityMap);
		}
		
		return result;
	}
	
	

	public CityMap get(String mapId) {
		CityMap cityMap = cityMapDao.get(mapId);
		return cityMap;
	}
	

	public List<CityMap> initMaps() {
		List<CityMap> cities = cityMapDao.initMaps();
		return cities;
	}

	/*public int updateMap(CityMap cityMap, Map<String, String> newMap) {
		int returnValue = "-1";
		//全部都要判断
		
		if (!StringUtils.isEmpty(cityMap.getNorth()) && !StringUtils.isEmpty(newMap.get("north_input"))) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("north_input"));
			newNode.setPlacedesc("");
			newNode.setSouth(cityMap.getId());
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setNorth(newNode.getId());
			}
		}
		if ((cityMap.getNortheast() != null && cityMap.getNortheast() == "-1") && newMap.get("northeast_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("northeast_input"));
			newNode.setPlacedesc("");
			newNode.setSouthwest(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setNortheast(newNode.getId());
			}
		}
		if ((cityMap.getNorthwest() != null && cityMap.getNorthwest() == "-1") && newMap.get("northwest_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("northwest_input"));
			newNode.setPlacedesc("");
			newNode.setSoutheast(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setNorthwest(newNode.getId());
			}
		}
		if ((cityMap.getEast() != null && cityMap.getEast() == "-1") && newMap.get("east_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("east_input"));
			newNode.setPlacedesc("");
			newNode.setWest(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setEast(newNode.getId());
			}
		}
		if ((cityMap.getWest() != null && cityMap.getWest() == "-1") && newMap.get("west_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("west_input"));
			newNode.setPlacedesc("");
			newNode.setEast(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setWest(newNode.getId());
			}
		}
		if ((cityMap.getSouth() != null && cityMap.getSouth() == "-1") && newMap.get("south_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("south_input"));
			newNode.setPlacedesc("");
			newNode.setNorth(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setSouth(newNode.getId());
			}
		}
		if ((cityMap.getSoutheast() != null && cityMap.getSoutheast() == "-1") && newMap.get("southeast_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("southeast_input"));
			newNode.setPlacedesc("");
			newNode.setNorthwest(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setSoutheast(newNode.getId());
			}
		}
		if ((cityMap.getSouthwest() != null && cityMap.getSouthwest() == "-1") && newMap.get("southwest_input") != null) {
			CityMap newNode = new CityMap();
			newNode.setCityid(cityMap.getCityid());
			newNode.setPlacename((String) newMap.get("southwest_input"));
			newNode.setPlacedesc("");
			newNode.setNortheast(Integer.valueOf(cityMap.getId()));
			returnValue = cityMapDao.mapBuild(newNode);
			if (returnValue == 1) {
				logger.debug("newNode.getId() = " + newNode.getId());
				cityMap.setSouthwest(newNode.getId());
			}
		}
		return cityMapDao.updateMap(cityMap);
	}

	public int mapBuild(CityMap cityMap) {
		return cityMapDao.mapBuild(cityMap);
	}*/
	
}
